/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.cx1.dao.api.ITransactionDao;
import org.cx1.dao.impl.TransactionDao;
import org.cx1.model.Transaction;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.ITransactionService;
import org.cx1.service.utils.IDGenerator;

/**
 *
 * @author ECH
 */
public class DTransactionService implements ITransactionService {

    private ITransactionDao transactionDao = new TransactionDao();
    
    @Override
    public ServiceResponse<String> create(Transaction transaction) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Transaction entry = new Transaction();
            if (StringUtils.isEmpty(transaction.getId())) {                
                id = IDGenerator.generateID(); 
            }
            else {
                id = transaction.getId();
            }                       
            entry.setId(id);
            entry.setType(transaction.getType());
            
            entry.setFeatured_listingID(transaction.getFeatured_listingID());
            entry.setFeatured_termType(transaction.getFeatured_termType());  
            entry.setFeatured_city(transaction.getFeatured_city());
                                    
            entry.setFeatured_startDate(transaction.getFeatured_startDate());
            entry.setFeatured_endDate(transaction.getFeatured_endDate());
               
            entry.setFeatured_strStartDate(transaction.getFeatured_strStartDate());
            entry.setFeatured_strEndDate(transaction.getFeatured_strEndDate());
                        
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            transactionDao.add(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse("");
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Transaction> read(String id) {
        ServiceResponse<Transaction> response = new ServiceResponse<Transaction>();
        Transaction transaction = null;
        try {            
            transaction = transactionDao.getById(id);
            response.setResponse(transaction);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> delete(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            transactionDao.deleteById(id);
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
        
}
